package com.example.ktapplication.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.View.OnClickListener
import android.view.ViewStub
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import com.example.ktapplication.R
import com.example.ktapplication.enum.StateLayoutMode
import com.example.ktapplication.utils.event.Event
import com.example.ktapplication.utils.event.EventBusUtil


class StatusLayout : FrameLayout {

    constructor(context: Context) : super(context) {
        init(context, null, 0)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init(context, attrs, 0)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context, attrs, defStyleAttr
    ) {
        init(context, attrs, defStyleAttr)
    }

    private var mLoadingLayoutResId = 0
    private var mEmptyLayoutResId = 0
    private var mErrorLayoutResId = 0
    private var mNetLayoutResId = 0

    private var mLoadingLayout: View? = null
    private var mEmptyLayout: View? = null
    private var mErrorLayout: View? = null
    private var mNetLayout: View? = null

    private var mContentLayoutContainer: FrameLayout? = null
    private var mLoadingLayoutContainer: LinearLayout? = null
    private var mEmptyLayoutContainer: LinearLayout? = null
    private var mErrorLayoutContainer: LinearLayout? = null
    private var mNetLayoutContainer: LinearLayout? = null

    init {
        init(context, null, 0);
    }

    private fun init(context: Context, attrs: AttributeSet?, defStyle: Int) {

        LayoutInflater.from(context).inflate(R.layout.status_layout, this)

        mContentLayoutContainer = findViewById<FrameLayout>(R.id.contentLayoutContainer)
    }

    override fun addView(child: View) {
        if (mContentLayoutContainer != null) {
            if (mContentLayoutContainer!!.childCount == 0) {
                mContentLayoutContainer!!.addView(child)
            } else {
                mContentLayoutContainer!!.removeAllViews()
                mContentLayoutContainer!!.addView(child)
            }
        } else {
            super.addView(child)
        }
    }


    override fun removeView(view: View) {
        if (mContentLayoutContainer == null) {
            super.removeView(view)
        } else {
            mContentLayoutContainer!!.removeView(view)
        }
    }

    fun setMode(stateMode: StateLayoutMode) {
        when (stateMode) {
            StateLayoutMode.LOADING -> {
                setLoadingLayoutVisibility(true)
                setEmptyLayoutVisibility(false)
                setErrorLayoutVisibility(false)
                setContentLayoutVisibility(false)
                setNetLayoutVisibility(false)
            }

            StateLayoutMode.EMPTY -> {
                setEmptyLayoutVisibility(true)
                setLoadingLayoutVisibility(false)
                setErrorLayoutVisibility(false)
                setContentLayoutVisibility(false)
                setNetLayoutVisibility(false)
            }

            StateLayoutMode.ERROR -> {
                setErrorLayoutVisibility(true)
                setEmptyLayoutVisibility(false)
                setLoadingLayoutVisibility(false)
                setContentLayoutVisibility(false)
                setNetLayoutVisibility(false)
            }

            StateLayoutMode.CONTENT -> {
                setContentLayoutVisibility(true)
                setEmptyLayoutVisibility(false)
                setLoadingLayoutVisibility(false)
                setErrorLayoutVisibility(false)
                setNetLayoutVisibility(false)
            }

            StateLayoutMode.NET -> {
                setNetLayoutVisibility(true)
                setContentLayoutVisibility(false)
                setEmptyLayoutVisibility(false)
                setLoadingLayoutVisibility(false)
                setErrorLayoutVisibility(false)
            }
        }
    }

    fun setLoadingLayout(loadingLayout: View?) {
        mLoadingLayout = loadingLayout
    }

    fun setEmptyLayout(emptyLayout: View?) {
        mEmptyLayout = emptyLayout
    }

    fun setErrorLayout(errorLayout: View?) {
        mErrorLayout = errorLayout
    }

    fun setNetLayout(netLayout: View?) {
        mNetLayout = netLayout
    }

    private fun setLoadingLayoutVisibility(visible: Boolean) {
        if (mLoadingLayoutContainer == null) {
            if (visible) {
                val viewStub = findViewById<View>(R.id.viewStub_loading) as ViewStub
                viewStub.inflate()
                mLoadingLayoutContainer = findViewById<LinearLayout>(R.id.status_loading)
                if (mLoadingLayout == null) {
                    LayoutInflater.from(context)
                        .inflate(mLoadingLayoutResId, mLoadingLayoutContainer)
                } else {
                    mLoadingLayoutContainer!!.addView(mLoadingLayout)
                }
                mLoadingLayoutContainer!!.visibility = VISIBLE
            }
        } else {
            mLoadingLayoutContainer!!.visibility = if (visible) VISIBLE else GONE
        }
    }

    private fun setEmptyLayoutVisibility(visible: Boolean) {
        if (mEmptyLayoutContainer == null) {
            if (visible) {
                val viewStub = findViewById<View>(R.id.viewStub_empty) as ViewStub
                viewStub.inflate()
                mEmptyLayoutContainer = findViewById<LinearLayout>(R.id.status_empty)
                if (mEmptyLayout == null) {
                    LayoutInflater.from(context).inflate(mEmptyLayoutResId, mEmptyLayoutContainer)
                } else {
                    mEmptyLayoutContainer!!.addView(mEmptyLayout)
                }
                mEmptyLayoutContainer!!.visibility = VISIBLE
            }
        } else {
            mEmptyLayoutContainer!!.visibility = if (visible) VISIBLE else GONE
            mEmptyLayoutContainer!!.setOnClickListener {
                if (mEmptyLayout == null)
                    EventBusUtil.eventBus.post(Event.HomeClick("ERROR"))
            }
        }
    }

    private fun setErrorLayoutVisibility(visible: Boolean) {
        if (mErrorLayoutContainer == null) {
            if (visible) {
                val viewStub = findViewById<View>(R.id.viewStub_error) as ViewStub
                viewStub.inflate()
                mErrorLayoutContainer = findViewById<LinearLayout>(R.id.status_error)
                if (mErrorLayout == null) {
                    LayoutInflater.from(context).inflate(mErrorLayoutResId, mErrorLayoutContainer)
                } else {
                    mErrorLayoutContainer!!.addView(mErrorLayout)
                }
                mErrorLayoutContainer!!.visibility = VISIBLE
            }
        } else {
            mErrorLayoutContainer!!.visibility = if (visible) VISIBLE else GONE
            mErrorLayoutContainer!!.setOnClickListener {
                if (mErrorLayout == null)
                    EventBusUtil.eventBus.post(Event.HomeClick("ERROR"))
            }
        }
    }

    private fun setNetLayoutVisibility(visible: Boolean) {
        if (mNetLayoutContainer == null) {
            if (visible) {
                val viewStub = findViewById<View>(R.id.viewStub_net) as ViewStub
                viewStub.inflate()
                mNetLayoutContainer = findViewById<LinearLayout>(R.id.status_net)
                if (mNetLayout == null) {
                    LayoutInflater.from(context).inflate(mNetLayoutResId, mNetLayoutContainer)
                } else {
                    mNetLayoutContainer!!.addView(mNetLayout)
                }
                mNetLayoutContainer!!.visibility = VISIBLE
            }
        } else {
            mNetLayoutContainer!!.visibility = if (visible) VISIBLE else GONE
            mNetLayoutContainer!!.setOnClickListener {
                if (mNetLayout == null)
                    EventBusUtil.eventBus.post(Event.HomeClick("ERROR"))
            }
        }
    }

    private fun setContentLayoutVisibility(visible: Boolean) {
        mContentLayoutContainer!!.visibility = if (visible) VISIBLE else GONE
    }
}